<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>专题管理</el-breadcrumb-item>
        <el-breadcrumb-item>专题列表</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud 
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel">
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_user_name }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>


<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appSubject as baseURL,appPopup, uploadFile } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: { 
        orderByList: "create_time&ASC",
      },
      option: {
        border: true,
        selection: true,
border: true,
        align: "center",
        headerAlign: "center",
        index: true,
        indexLabel: "编号",
        column: [
          {
            label: "专题id",
            prop: "subject_id",
            type: "input",
            search:true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "专题名称",
            prop: "subject_name",
            type: "input",
            search:true,
          },
          {
            label: "专题类型",
            prop: "subject_type",
            type: "select",
            dicData: [
              {
                label: "签约专题",
                value: 1,
              },
              {
                label: "科普专题",
                value: 2,
              }
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "连接",
            prop: "link_url",
            type: "input",
          },
          {
            label: "状态",
            prop: "is_online",
            type: "select",
            dicData: [
              {
                label: "上线",
                value: 1,
              },
              {
                label: "下线",
                value: 0,
              }
            ], 
            search:true,
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            type: "upload",
            label: "图片",
            span: 12,
            display: true,
            showFileList: true,
            multiple: false,
            limit: 10,
            canvasOption: {},
            prop: "subject_image",
            drag: false,
            listType: "picture-img",
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "图片必须上传",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  }, 
  methods: {
     /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        {...this.params,...this.default},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
       
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo
      row.edit_user_name = userInfo.username;
      row.edit_time = new Date();
      row.edit_user_id = userInfo.id;
      let data = await common(`${baseURL.save}`, row, null, "POST");
      if (data == null) {
        return 
      }
      
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo
      row.edit_user_name = userInfo.username;
      row.edit_time = new Date();
      row.edit_user_id = userInfo.id;
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        return 
      }
      
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
      
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.subject_id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>


